namespace iScadaFBD
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [Serializable]
    public abstract class FBObject : DrawObject
    {
        private ArrayList attachConnections;
        private int executeOrder;
        private iScadaFBD.GlobalParam.FBType fbType;
        private int inCount;
        private ArrayList inNodeList;
        private int outCount;
        private ArrayList outNodeList;

        public FBObject()
        {
            base.ObjectType = GlobalParam.ObjectType.FunctionBlock;
            this.inNodeList = new ArrayList();
            this.outNodeList = new ArrayList();
            this.attachConnections = new ArrayList();
            base.GenerateID(GlobalParam.ObjectType.FunctionBlock);
        }

        public void DrawBackground(int InNo, int OutNo, Graphics g)
        {
            Rectangle rect = new Rectangle(base.FBRect.X + 1, base.FBRect.Y + 1, base.FBRect.Width - 2, base.FBRect.Height - 2);
            Pen pen = new Pen(Color.Black, 1f);
            SolidBrush brush = new SolidBrush(Color.FromArgb(0xd0, 0xd0, 0xd0));
            Font font = new Font("Bold", 7f);
            SolidBrush brush2 = new SolidBrush(Color.Brown);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            g.DrawRectangle(pen, base.FBRect);
            pen.Color = Color.White;
            g.DrawRectangle(pen, rect);
            g.FillRectangle(brush, rect);
            int num = ((((rect.Top + 15) + 10) + (8 * Math.Max(this.InCount, this.OutCount))) + (8 * (Math.Max(this.InCount, this.OutCount) - 1))) - 2;
            g.DrawLine(pen, rect.Left, rect.Top, rect.Right, rect.Top);
            g.DrawLine(pen, rect.Left, rect.Top, rect.Left, rect.Bottom);
            pen.Color = Color.FromArgb(100, 100, 100);
            g.DrawLine(pen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
            g.DrawLine(pen, rect.Right, rect.Top, rect.Right, rect.Bottom);
            pen.Color = Color.FromArgb(100, 100, 100);
            g.DrawLine(pen, rect.Left, rect.Top + 15, rect.Right - 1, rect.Top + 15);
            g.DrawLine(pen, rect.Left, num, rect.Right - 1, num);
            pen.Color = Color.White;
            g.DrawLine(pen, rect.Left, (rect.Top + 15) + 1, rect.Right - 1, (rect.Top + 15) + 1);
            g.DrawLine(pen, rect.Left, num + 1, rect.Right - 1, num + 1);
            g.DrawString(base.ObjName, font, brush2, this.FontRect(font.Height, 8), format);
            g.DrawString(this.FBType.ToString(), font, brush2, this.FBTypeRect(font.Height, 8), format);
            int num2 = base.FBRect.Left + (base.FBRect.Width / 2);
            Rectangle layoutRectangle = new Rectangle(num2 - ((font.Height * 8) / 2), (base.FBRect.Bottom - font.Height) - 1, font.Height * 8, font.Height);
            brush2.Color = Color.Blue;
            g.DrawString("#" + this.ExecuteOrder.ToString(), font, brush2, layoutRectangle, format);
            pen.Dispose();
            brush.Dispose();
            font.Dispose();
            format.Dispose();
            brush2.Dispose();
        }

        private Rectangle FBTypeRect(int fontHeight, int CharNo)
        {
            int num = base.FBRect.Left + (base.FBRect.Width / 2);
            return new Rectangle(num - ((fontHeight * CharNo) / 2), base.FBRect.Top - fontHeight, fontHeight * CharNo, fontHeight);
        }

        private Rectangle FontRect(int fontHeight, int CharNo)
        {
            int num = base.FBRect.Left + (base.FBRect.Width / 2);
            return new Rectangle(num - ((fontHeight * CharNo) / 2), base.FBRect.Top + 3, fontHeight * CharNo, fontHeight);
        }

        public override Point GetHandle(int handleNumber)
        {
            int x = base.FBRect.X;
            int y = base.FBRect.Y;
            switch (handleNumber)
            {
                case 1:
                    x = base.FBRect.X;
                    y = base.FBRect.Y;
                    break;

                case 2:
                    x = base.FBRect.Right;
                    y = base.FBRect.Y;
                    break;

                case 3:
                    x = base.FBRect.Right;
                    y = base.FBRect.Bottom;
                    break;

                case 4:
                    x = base.FBRect.X;
                    y = base.FBRect.Bottom;
                    break;
            }
            return new Point(x, y);
        }

        public override Cursor GetHandleCursor(int handleNumber)
        {
            switch (handleNumber)
            {
                case 1:
                    return Cursors.SizeNWSE;

                case 2:
                    return Cursors.SizeNESW;

                case 3:
                    return Cursors.SizeNWSE;

                case 4:
                    return Cursors.SizeNESW;
            }
            return Cursors.Default;
        }

        public override int HitTest(Point point)
        {
            if (base.Selected)
            {
                for (int i = 1; i <= this.HandleCount; i++)
                {
                    if (this.GetHandleRectangle(i).Contains(point))
                    {
                        return i;
                    }
                }
            }
            if (this.PointInObject(point))
            {
                return 0;
            }
            return -1;
        }

        public override bool IntersectsWith(Rectangle rectangle)
        {
            return rectangle.Contains(base.FBRect);
        }

        public Point LinkInPoint(int InNo)
        {
            return new Point(base.FBRect.X, (((base.FBRect.Y + 15) + 5) + (0x10 * InNo)) + 4);
        }

        public Point LinkOutPoint(int OutNo)
        {
            return new Point(base.FBRect.Right, (((base.FBRect.Y + 15) + 5) + (0x10 * OutNo)) + 4);
        }

        public override void Move(int deltaX, int deltaY)
        {
            base.FBRect = new Rectangle(base.FBRect.X + deltaX, base.FBRect.Y + deltaY, base.FBRect.Width, base.FBRect.Height);
        }

        public override void MoveHandleTo(Point point, int handleNumber)
        {
            int left = base.FBRect.Left;
            int top = base.FBRect.Top;
            int right = base.FBRect.Right;
            int bottom = base.FBRect.Bottom;
            switch (handleNumber)
            {
                case 2:
                    if (((point.X - left) > 50) && ((point.X - left) < 180))
                    {
                        right = point.X;
                    }
                    break;

                case 3:
                    if (((point.X - left) > 50) && ((point.X - left) < 180))
                    {
                        right = point.X;
                    }
                    break;
            }
            this.SetRectangle(left, top, right - left, bottom - top);
        }

        public override bool PointInObject(Point point)
        {
            return base.FBRect.Contains(point);
        }

        protected void SetRectangle(int x, int y, int width, int height)
        {
            base.FBRect = new Rectangle(x, y, width, height);
        }

        [Browsable(false)]
        public ArrayList AttachConnections
        {
            get
            {
                return this.attachConnections;
            }
            set
            {
                this.attachConnections = value;
            }
        }

        [Browsable(false)]
        public int ExecuteOrder
        {
            get
            {
                return this.executeOrder;
            }
            set
            {
                this.executeOrder = value;
            }
        }

        [Browsable(false)]
        public iScadaFBD.GlobalParam.FBType FBType
        {
            get
            {
                return this.fbType;
            }
            set
            {
                this.fbType = value;
            }
        }

        public override int HandleCount
        {
            get
            {
                return 4;
            }
        }

        [Browsable(false)]
        public int InCount
        {
            get
            {
                return this.inCount;
            }
            set
            {
                this.inCount = value;
            }
        }

        [Browsable(false)]
        public ArrayList InNodeList
        {
            get
            {
                return this.inNodeList;
            }
            set
            {
                this.inNodeList = value;
            }
        }

        [Browsable(false)]
        public int OutCount
        {
            get
            {
                return this.outCount;
            }
            set
            {
                this.outCount = value;
            }
        }

        [Browsable(false)]
        public ArrayList OutNodeList
        {
            get
            {
                return this.outNodeList;
            }
            set
            {
                this.outNodeList = value;
            }
        }
    }
}

